<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/3
 * Time: 14:02
 */

namespace app\models;

use yii;
use yii\db\Query;

class InrepoLog extends BaseModel
{
    public function __construct()
    {
        $this->table = yii::$app->db->tablePrefix . 'inrepo_log';
    }

    public function all($where = [], $offset = false, $limit = false, $order = false)
    {
        $db = new Query();
        if (!empty($where['status'])) {
            $db->andWhere(['status' => $where['status']]);
        }
        if (!empty($where['begin_time'])) {
            $db->andWhere(['>=', 'date', $where['begin_time']]);
        }
        if (!empty($where['end_time'])) {
            $db->andWhere(['>=', 'date', $where['end_time']]);
        }
        if (!empty($where['invoice'])) {
            if (is_array($where['invoice']))
                $db->andWhere(['in', 'invoice', $where['invoice']]);
            else
                $db->andWhere(['invoice' => $where['invoice']]);
        }

        if (!empty($where['inrepo_id'])) $db->andWhere(['inrepo_id' => $where['inrepo_id']]);
        $db->select('*')->from($this->table);
        if ($offset) $db->offset($offset);
        if ($limit) $db->limit($limit);
        if ($order) $db->orderBy($order);
        return $db->all();
    }

    public function size($where = [])
    {
        $db = new Query();
        $db->select('count(*) as num')->from($this->table);
        if (!empty($where['status'])) {
            $db->andWhere(['status' => $where['status']]);
        }
        if (!empty($where['begin_time'])) {
            $db->andWhere(['>=', 'date', $where['begin_time']]);
        }
        if (!empty($where['end_time'])) {
            $db->andWhere(['>=', 'date', $where['end_time']]);
        }
        if (!empty($where['invoice'])) {
            if (is_array($where['invoice']))
                $db->andWhere(['in', 'invoice', $where['invoice']]);
            else
                $db->andWhere(['invoice' => $where['invoice']]);
        }

        if (!empty($where['inrepo_id'])) $db->andWhere(['inrepo_id' => $where['inrepo_id']]);
        $re = $db->one();
        return $re ? $re['num'] : 0;
    }

    public function one($where = [])
    {

    }


}